/**
 * Copyright &copy; 2021-2026 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.sys.favMenu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.sys.favMenu.domain.FavMenu;
import com.jeeplus.sys.favMenu.mapper.FavMenuMapper;
import com.jeeplus.sys.favMenu.service.FavMenuService;
import com.jeeplus.sys.favMenu.service.dto.FavMenuDTO;
import com.jeeplus.sys.favMenu.service.mapstruct.FavMenuWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * 收藏菜单ServiceImpl
 *
 * @author jeeplus
 * @version 2023-09-05
 */
@Service
@Transactional
public class FavMenuServiceImpl extends ServiceImpl<FavMenuMapper, FavMenu> implements FavMenuService {

    @Autowired
    private FavMenuWrapper favMenuWrapper;

    @Autowired
    private FavMenuMapper favMenuMapper;

    @Override
    public List<FavMenuDTO> findByUserId(String userId) {
        return favMenuMapper.findByUserId(userId);
    }

    @Override
    public boolean addFavMenu(String userId, String menuId) {
        // 检查是否已经收藏
        if (isFaved(userId, menuId)) {
            return true; // 已经收藏过，直接返回成功
        }

        // 创建新的收藏记录
        FavMenu favMenu = new FavMenu();
        favMenu.setId(UUID.randomUUID().toString());
        favMenu.setUserId(userId);
        favMenu.setMenuId(menuId);
        
        return save(favMenu);
    }

    @Override
    public boolean removeFavMenu(String userId, String menuId) {
        favMenuMapper.deleteByUserIdAndMenuId(userId, menuId);
        return true;
    }

    @Override
    public boolean isFaved(String userId, String menuId) {
        return favMenuMapper.countByUserIdAndMenuId(userId, menuId) > 0;
    }
}